<?php
	
	// Session
	session_start();
	
	// Nese vizitori eshte i identifikuar
	if (!isset($_SESSION['user_id'])) {
		header("Location: login.php");
	}
	
	// Integrimi i skedave sistemore
    include('../include/functions.inc.php');
	include('../include/template.class.php');
	
	// Project
	$PROJECT = setProject();
	
	
	// Ndyshoret primare
	define('TPL','../include/styles/'.$PROJECT['style'].'/members/userprofil.html');
	define('UID',$_SESSION['user_id']);
	define('FILENAME',basename($_SERVER["PHP_SELF"]));
	
	
	// Connection
	dbOpen("comunity");
	
	// Inicializimi i shablonave
	$tmp = new Template(TPL,$PROJECT['lang']);
	
	// SPLASH SCREEN
	if (isset($_GET['splash'])) 
		splash($_GET['splash'], "../members/userprofil.php");
	
	
	// Te dhenat e vizitorit
	$USER = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE id = '".UID."'")); 
	$USER_FIX['name'] = $USER['name'];
	$USER_FIX['nickname'] = $USER['nickname'];
	$USER_FIX['birthdate'] = setDate($USER['birthdate']);
	$USER_FIX['sex'] = ($USER['sex'] == 1) ? "|mister|" : "|misses|";
	$USER['sendnews'] = ($USER['sendnews'] == 1) ? "checked='checked'" : "";
	$USER['anonym'] = ($USER['anonym'] == 1) ? "checked='checked'" : "";
	
	
	// Zevendeso vetem vlerat qe nyk ndyshojne
	$tmp->setContent($USER_FIX);

	
	// Nese forma akoma nuk eshte aplikuar
	if (!isset($_POST['send'])) 
		$_POST = $USER;
	
	
	// OPTIONS
	$tmp->setOptions($_POST);
	
	// LABELS
	$tmp->setLabels();
	
	// MENUES
	$tmp->setContent("CATEGORY",$tmp->menu("category","../members")); 
	$tmp->setContent("MYMENU",$tmp->menu("mymenu","userprofil.php"));
	$tmp->setContent("FOOTMENU",$tmp->menu("foot"));
	$tmp->setContent("MYPROFIL",$tmp->menu("myprofil",FILENAME));
	
	
	// ACCORDIONS
	$tmp->setContent("USERPROFIL",$tmp->xmlContent("userprofil","help.xml"));
	$tmp->setContent("USERDESCRIPT",$tmp->xmlContent("userdescript","help.xml"));
	$tmp->setContent("USERIMAGE",$tmp->xmlContent("userimage","help.xml"));

	
	
	// Aplikimi i formes
	if(isset($_POST['send'])) {
		
		// NULL PROFESSION
		if(empty($_POST['profession']))
			$ERROR['profession'] = "null";
		
		// NULL CITY
		if(empty($_POST['place'])) 
			$ERROR['place'] = "null";
		elseif(is_numeric($_POST['place']))
			$ERROR['place'] = "value";
		
		// NULL STATE
		if(empty($_POST['state']))
			$ERROR['state'] = "null";
		
		// NULL EMAIL
		if(empty($_POST['email'])) 
			$ERROR['email'] = "null";
		// SYNTAX EMAIL
		elseif(!ereg("^[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[@]{1}[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[.]{1}[A-Za-z]{2,5}$", $_POST['email']))
			$ERROR['email'] = "syntax";
		// IF EMAIL CHANGE
		elseif($_POST['email'] != $USER['email']) {
			// Kontrollo valle a ekziston emaili ne arkiven e vizitoreve
			$query = mysql_query("SELECT id FROM users WHERE email = '".$_POST['email']."'");
			
			if (mysql_num_rows($query) > 0)
				$ERROR['email'] = "double";
		}
		
		// PASSWORD
		if(empty($_POST['password'])) 
			$ERROR['password'] = "null";
		elseif(strlen($_POST['password']) > 16 || strlen($_POST['password']) < 6)
			$ERROR['password'] = "syntax";
			
		
		// STYLE
		// PASSWORD
		if(empty($_POST['style'])) 
			$ERROR['style'] = "null";
			
		
		// Nese nuk ka asnje gabim
		if(empty($ERROR)) {
			
			// Arkivimi i vlerave
			$query = "UPDATE users SET 
								place = '".$_POST['place']."',
								profession = '".$_POST['profession']."',
								state = '".$_POST['state']."',
								street = '".$_POST['street']."',
								nr = '".$_POST['nr']."',
								plz = '".$_POST['plz']."',
								province = '".$_POST['province']."',
								tel = '".$_POST['tel']."',
								email = '".$_POST['email']."',
								password = '".$_POST['password']."',
								sendnews = '".$_POST['sendnews']."',
								anonym = '".$_POST['anonym']."',
								style = '".$_POST['style']."'
						WHERE id = '".UID."' ";
			
			// Nese te dhenat aktualizohen, trego splash porosin
			if(mysql_query($query)) {
				
				// Rilexo faqen me porosin per sukses
				header('Location: userprofil.php?splash=userupdate'); 
				exit;
			}
			else {
				
				// Rilexo faqen me porosin per sukses
				header('Location: userprofil.php?splash=busy'); 
				exit;
			}
		}
	}
	
	// Convert POST and ERROR vars in Form
	$tmp->setForm($_POST,$ERROR);

	
	// Dhe cdo gje ka nje fund, trego faqen						  
	echo $tmp->vorlage;
	
?>